package com.qys.livesMall.shop.scheduledTask.processor.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.shop.constants.OrderStatus;
import com.qys.livesMall.shop.constants.PayStatus;
import com.qys.livesMall.shop.mapper.ShopGoodsSkuStocksMapper;
import com.qys.livesMall.shop.modal.entity.SeckillOrder;
import com.qys.livesMall.shop.modal.entity.ShopOrderIdea;
import com.qys.livesMall.shop.modal.entity.ShopOrderInfo;
import com.qys.livesMall.shop.modal.entity.ShopOrderSku;
import com.qys.livesMall.shop.scheduledTask.processor.OrderDelayTaskProcessor;
import com.qys.livesMall.shop.scheduledTask.taksEnum.TaskTypeEnum;
import com.qys.livesMall.shop.scheduledTask.task.OrderTaks;
import com.qys.livesMall.shop.service.SeckillOrderService;
import com.qys.livesMall.shop.service.ShopOrderIdeaService;
import com.qys.livesMall.shop.service.ShopOrderInfoService;
import com.qys.livesMall.shop.service.ShopOrderSkuService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @packageName:
 * @className:OrderDelayTaskProcessor
 * @Description:
 * @author:guomx
 * @date:2023/7/2914:47
 */
@Component
public class OrderDelayTaskProcessorImpl implements OrderDelayTaskProcessor {
    private final static Logger log = LoggerFactory.getLogger(OrderDelayTaskProcessorImpl.class);
    @Resource
    private ShopOrderInfoService infoService;
    @Resource
    private ShopOrderSkuService orderSkuService;
    @Resource
    private ShopOrderIdeaService ideaService;
    @Resource
    private ShopGoodsSkuStocksMapper skuStocksMapper;
    @Resource
    private SeckillOrderService seckillOrderService;
    @Override
    public TaskTypeEnum supportTask() {
        return TaskTypeEnum.Order;
    }

    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void process(OrderTaks task) {
        String threadName = Thread.currentThread().getName();
        log.info("异步执行的线程名：" + threadName);
        ShopOrderInfo orderInfo = infoService.getById(task.getOrderId());
        if (ObjectUtils.isNotEmpty(orderInfo)){
            if (!orderInfo.getOrderStatus().equals(OrderStatus.CANCELED)&&!orderInfo.getPayStatus().equals(PayStatus.HAVE_PAYMENT)){
                LambdaQueryWrapper<ShopOrderSku> orderSkuQueryWrapper = new LambdaQueryWrapper<>();
                orderSkuQueryWrapper.eq(ShopOrderSku::getOrderId,orderInfo.getId());
                List<ShopOrderSku> orderSkus = orderSkuService.list(orderSkuQueryWrapper);
                if (ObjectUtils.isNotEmpty(orderSkus)){
                    for (ShopOrderSku orderSku : orderSkus) {
                        orderSku.setOrderStatus(OrderStatus.CANCELED);
                        orderSkuService.updateById(orderSku);
                        Long stocksId = orderSku.getStocksId();
                        Integer num = orderSku.getNum();
                        skuStocksMapper.decount(stocksId,-num);
                    }
                    orderInfo.setOrderStatus(OrderStatus.CANCELED);
                    if(infoService.updateById(orderInfo)){
                        //秒杀订单
                        SeckillOrder seckillOrder=seckillOrderService.getById(orderInfo.getId());
                        if (seckillOrder!=null){
                            seckillOrder.setStatus(PayStatus.PAYMENT_FAILURE);
                            seckillOrderService.updateById(seckillOrder);
                        }

                        ShopOrderIdea idea = new ShopOrderIdea();
                        idea.setIdea(MessageUtils.message("order.overdue"));
                        idea.setOrderId(orderInfo.getId());
                        idea.setDescription(MessageUtils.message("order.overdue")+".1");
                        idea.setCreateTime(System.currentTimeMillis());
                        ideaService.saveOrUpdate(idea);
                    }
                }
            }
        }
        // 执行取消订单的逻辑
        log.info("Cancel order:[{}]",task.getOrderId());
        log.info(threadName+"执行成功");
    }
}
